package com.ybu.oodd.controller;

import com.ybu.oodd.myenum.ErrorType;
import com.ybu.oodd.pojo.*;
import com.ybu.oodd.service.UserService;
import com.ybu.oodd.service.impl.UserServiceImpl;
import com.ybu.oodd.utils.login.ContextHolder;
import com.ybu.oodd.utils.oss.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /*
     * test
     * */
    @PostMapping("/test")
    public Result test(){
        return Result.success("成功"+ ContextHolder.get("userId"),userService.getFreeUserId());
    }

    /*
    * 用户登录
    * */
    @PostMapping("/login")
    public Result login(@RequestBody UserLogin userLogin){
        log.info("用户登录请求{}",userLogin);
        return userService.login(userLogin);
    }

    /*
     * 用户注册
     * */
    @PostMapping("/register")
    public Result register(@RequestBody UserLogin userLogin){
        log.info("用户注册请求{}",userLogin);
        ErrorType errorType = userService.register(userLogin);
        if(errorType==null){
            return Result.success("注册成功");
        }else{
            return Result.error(errorType.toString());
        }
    }

    /*
    * 获取验证码请求
    * */
    @PostMapping("/code")
    public Result getCode(@RequestBody UserLogin userLogin){
        log.info("获取验证码请求{}",userLogin);
        ErrorType errorType = userService.getCode(userLogin);
        if(errorType==null){
            return Result.success("获取验证码成功");
        }else{
            return Result.error(errorType.toString());
        }
    }

    /*
     * 修改密码
     * */
    @PutMapping("/update/password")
    public Result updatePassword(@RequestBody UserLogin userLogin){
        log.info("修改密码请求{}",userLogin);
        ErrorType errorType = userService.updatePassword(userLogin);
        if(errorType==null){
            return Result.success("修改密码成功");
        }else{
            return Result.error(errorType.toString());
        }
    }

    /*
     * 修改密码时，获取验证
     * */
    @PostMapping("/update/code")
    public Result updatePasswordGetCode(@RequestBody UserLogin userLogin){
        log.info("修改密码时，获取验证请求{}",userLogin);
        ErrorType errorType = userService.updatePasswordGetCode(userLogin);
        if(errorType==null){
            return Result.success("获取验证码成功");
        }else{
            return Result.error(errorType.toString());
        }
    }

    /*
     * 查询用户信息
     * */
    @GetMapping("/info")
    public Result getUserInfo(){
        log.info("修改密码时，获取验证请求");
        User user = userService.getUserInfo();
        return Result.success("查询用户信息成功",user);
    }

    /*
     * 用户添加问题
     * */
    @PostMapping("/problem")
    public Result saveUserProblem(@RequestBody Problem problem){
        log.info("用户添加问题{}",problem);
        userService.saveUserProblem(problem);
        return Result.success("用户添加问题成功");
    }

    /*
     * 用户查询问题
     * */
    @GetMapping("/problem")
    public Result getUserProblem(){
        log.info("用户查询问题");
        List<Problem> list = userService.getUserProblem();
        return Result.success("用户查询问题成功",list);
    }

    /*
     * 用户修改问题
     * */
    @PutMapping("/problem")
    public Result updateUserProblem(@RequestBody Problem problem){
        log.info("用户修改问题{}",problem);
        userService.updateUserProblem(problem);
        return Result.success("用户修改问题成功");
    }

    /*
     * 用户删除问题
     * */
    @DeleteMapping("/problem/{id}")
    public Result deleteUserProblem(@PathVariable Integer id){
        log.info("用户删除问题:{}",id);
        userService.deleteUserProblem(id);
        return Result.success("用户删除问题成功");
    }

    /*
    * 用户上传文件
    * */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("上传文件：{}", file);
        Pictures p = userService.upload(file);
        return Result.success("上传成功",p);
    }

    /*
    * 根据id和user_id 查询url
    * */
    @GetMapping("/picture")
    public Result getPictureUrl(@RequestParam("ids") List<Integer> ids){
        log.info("查询id为{}的图片",ids);
        List<Pictures> list = userService.getPictureUrl(ids);
        return Result.success("查询成功",list);
    }
}
